package com.tang.dao.impl;

import com.tang.bean.Book;
import com.tang.dao.OrderDao;

import com.tang.bean.Order;
import com.tang.dao.BaseDao;
import com.tang.utils.JDBCUtil;
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl extends BaseDao<Order> implements OrderDao {

  @Override
  public Integer insertOrder(Order order) throws Exception {
    try{
      String sql = "insert into t_order (order_sequence,create_time,total_count,total_amount,order_status,user_id) values (?,?,?,?,?,?)";
      //因为使用DBUtils执行增删改的SQL语句没法获取自增⻓的id主键，所以我们只能使用原始的JDBC执行这个添 加数据的SQL语句并且获取自增⻓的id
      //1. 获取连接
      Connection conn = JDBCUtil.getConnection();
      //2. 预编译SQL语句
      PreparedStatement preparedStatement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
      //3. 设置问号处的参数
      preparedStatement.setObject(1, order.getOrderSequence());
      preparedStatement.setObject(2, order.getCreateTime());
      preparedStatement.setObject(3, order.getTotalCount());
      preparedStatement.setObject(4, order.getTotalAmount());
      preparedStatement.setObject(5, order.getOrderStatus());
      preparedStatement.setObject(6, order.getUserId());
      //4. 执行SQL语句
      preparedStatement.executeUpdate();
      //5. 获取自增⻓的主键值
      ResultSet rst = preparedStatement.getGeneratedKeys();
      //因为自增⻓的主键只有一个值，所以不需要while循环遍历
      int orderId = 0;
      if (rst.next()) {
        orderId = rst.getInt(1);
      }
      return orderId;
    } catch (SQLException e) {
      e.printStackTrace();
      throw new RuntimeException(e.getMessage());
    }
  }

  @Override
  public Integer getOrderCount(Integer userId) throws Exception {
    String sql="select count(*) from t_order WHERE user_id = ?";
    Integer  count=(int)getBean(sql,userId);
    return count;
  }

  @Override
  public List<Order> getPageItems(Integer pageBegin, Integer maxPageSize, Integer userId) throws Exception {
    String sql="select order_id orderId,order_sequence orderSequence,create_time createTime,total_count totalCount,total_amount totalAmount,order_status orderStatus,user_id userId from t_order where user_id=? limit ?,?";
    return getBeanList(Order.class,sql,userId,pageBegin,maxPageSize);
  }

  @Override
  public Integer getOrderCountByManager() throws Exception {
    String sql="select count(*) from t_order";
    Integer  count=(int)getBean(sql);
    return count;
  }

  @Override
  public List<Order> getPageItemsByManager(Integer pageBegin, Integer maxPageSize) throws Exception {
    String sql="select order_id orderId,order_sequence orderSequence,create_time createTime,total_count totalCount,total_amount totalAmount,order_status orderStatus,user_id userId from t_order limit ?,?";
    return getBeanList(Order.class,sql,pageBegin,maxPageSize);
  }

  @Override
  public void changeOrderStatus(Integer orderId,Integer orderStatus) throws Exception {
    String sql="UPDATE t_order SET order_status=? WHERE order_id=?";
    update(sql,orderStatus+1,orderId);
  }

  @Override
  public void orderCancel(Integer orderId) throws Exception {
    String sql="UPDATE t_order SET order_status=4 WHERE order_id=?";
    update(sql,orderId);
  }
}

